package com.xlh.server.protocol.support.json;

import android.text.Html;
import android.view.View;
import android.widget.AdapterView;

import com.xlh.Utils.FontHandler;
import com.xlh.Utils.GsonManager;
import com.xlh.adapter.GridViewAndListViewAdapter;
import com.xlh.bean.ProtocolObject.LongBoxRB;
import com.xlh.bean.TitleBean;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Created by mike@Q184377367 on 2018/7/2. on 2018/7/2.
 */

public class LongBoxRBServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "longBoxRB";
    private LongBoxRB protccoObj;
    public LongBoxRBServer(IActivity atv){
        this.atv = atv;


    }

    @Override
    public void takeMessage(String t,String s){

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, LongBoxRB.class);

        if(protccoObj==null || protccoObj.getRightButList().size()<=0) return;

        this.renderView(protccoObj);
    }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit() {

    }

    public void renderView(LongBoxRB longBoxRB){

        final LongBoxRBServer me = this;
        //列数 默认三列
        int gridConums = 3;
        String[] strData = new String[]{"",""} ;
        String label,cmd;


        List<TitleBean> data = new ArrayList<TitleBean>();
        data.clear();

        gridConums = longBoxRB.getButStyle().getColNumber();




        for (int i = 0; i < longBoxRB.getRightButList().size(); i++) {

            TitleBean t = new TitleBean();
            label = longBoxRB.getRightButList().get(i).getLabel();
            cmd = longBoxRB.getRightButList().get(i).getCmd();
            t.setSpanned(this.atv.getActivity().getHtmlSpanner().fromHtml(label));

            t.setmAction(cmd);
            data.add(t);
        }

        if (me.atv.getActivity().getmAlertLayout().getVisibility() == View.INVISIBLE) {
                    me.atv.getActivity().getmAlertLayout().setVisibility(View.VISIBLE);
                    me.atv.getActivity().getmAlertText().setText("");
        }

        if ( me.atv.getActivity().getmAlertList().getDataAdapter().getCount() <= 0) {
            me.atv.getActivity().getmAlertList().setVisibility(View.GONE);
        }else{
            me.atv.getActivity().getmAlertList().setVisibility(View.VISIBLE);
        }


        me.atv.getActivity().getmAlertGrid().getDataAdapter().setGridConums(gridConums);
        me.atv.getActivity().getmAlertGrid().getDataAdapter().setParentWidth(me.atv.getActivity().getmAlertGrid().getWidth());
        me.atv.getActivity().getmAlertGrid().getDataAdapter()
                .setDatas(data)
                .update();


        if(  me.atv.getActivity().getmAlertGrid().getDataAdapter().getCount()>0 ){
            me.atv.getActivity().getmAlertLayout().setVisibility(View.VISIBLE);
        }
        me.atv.getActivity().getmAlertGrid().setNumColumns(gridConums);

        me.atv.getActivity().getmAlertGrid().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                // TODO Auto-generated method stub


                me.atv.getActivity().getmAlertLayout().setVisibility(View.INVISIBLE);
                me.atv.getActivity().getmAlertText().setText("");


                me.atv.getActivity().mSocketClient.sendMsg(me.atv.getActivity()
                        .getmAlertGrid()
                        .getDataAdapter()
                        .getItem(position).getmAction());

                me.atv.getActivity().getmAlertGrid().getDataAdapter().clear();
                me.atv.getActivity().getmAlertList().getDataAdapter().clear();
                me.atv.getActivity().lvFloatRightList.setVisibility(View.VISIBLE);
                me.atv.getActivity().closeAlertBox();
            }
        });

    }






}
